<%@ page contentType="text/html;charset=UTF-8"%>
<script type="text/javascript">
	//是否服务器分页
	var _isServerPage = true;
	//表格列定义
	var _tableCols = 	[{
		data : "uuid",
		orderable : false,
		className : "center",
		width : "30",
		render : RapDataTableUtils.renderCheckCol
	},
	{
		data : 'contract.contractNum',
		width : "120",
		render : function(data, type, full){
			return "<a href='#' onclick='onViewContract(this,\""+full.contract.uuid+"\");'>"+data+"</a>";
		},
		orderable : true
	},
	{
		data : 'yearMonth',
		width : "120",
		//render : function(data, type, full){},
		orderable : true
	},
	{
		data : 'contract.custom.theName',
		width : "120",
		//render : function(data, type, full){},
		orderable : true
	},
	{
		data : 'total',
		width : "120",
		className : "rap-td-number",
		render:milliFormat,
		orderable : true
	},
	{
		data : "uuid",
		className : "center",
		orderable : false,
		render : function(data, type, full) {
				return "<div class='rap-btn-actiongroup'>" 
						+ "<button id='rap-btn-view-row' class='btn btn-xs btn-success' data-rel='tooltip' title='查看'><i class='fa fa-search-plus'></i></button>"
						<shiro:hasPermission name="stwy:bills:update">
						+ "<button id='rap-btn-edit-row' class='btn btn-xs btn-info' data-rel='tooltip' title='编辑'><i class='fa fa-edit'></i></button>"
						</shiro:hasPermission>
						<shiro:hasPermission name="stwy:bills:delete">
						+ "<button id='rap-btn-remove-row' class='btn btn-xs btn-danger' data-rel='tooltip' title='删除'><i class='fa fa-trash-o'></i></button>"
						</shiro:hasPermission>
						+ "</div>";
			},
		width : "80"
	}]
;
	function onViewContract(obj,uuid){
		$(obj).colorbox({
			iframe:true,
			opacity	: 0.2,
			overlayClose:true, 
			transition : 'none',
			speed : 200,
			href : "${ctx}/lease/contract/view?uuid="+uuid,
			innerWidth : '90%',
			innerHeight : '85%',
			close : '×',
			fixed : true
		});
	}
	//宏：生成ShowData方法，在编辑框显示记录数据
	function showData(data) {
		$("input[name='uuid']").val(data.uuid);
		
		$("input[name='contract.uuid']").val(data.contract.uuid);
		$("input[name='contractNum']").val(data.contract.contractNum);
		
		$("input[name='yearMonth']").val(data.yearMonth);

		$("input[name='org1']").val(data.org1);
			
		$("input[name='bank1']").val(data.bank1);
			
		$("input[name='bankNum1']").val(data.bankNum1);
			
		$("input[name='phone1']").val(data.phone1);
			
		$("input[name='org2']").val(data.org2);
			
		$("input[name='bankNum2']").val(data.bankNum2);
			
		$("input[name='area']").val(data.area);
			
			//水电费
		$("input[name='row1Col1']").val(data.row1Col1);
		$("input[name='row1Col2']").val(data.row1Col2);
		$("input[name='row1Col3']").val(data.row1Col3);
		$("input[name='row1Col4']").val(data.row1Col4);
		$("input[name='row1Col5']").val(data.row1Col5);
		$("input[name='row1Col6']").val(data.row1Col6);
		$("input[name='row1Col7']").val(data.row1Col7);
			
		$("input[name='row2Col1']").val(data.row2Col1);
		$("input[name='row2Col2']").val(data.row2Col2);
		$("input[name='row2Col3']").val(data.row2Col3);
		$("input[name='row2Col4']").val(data.row2Col4);
		$("input[name='row2Col5']").val(data.row2Col5);
		$("input[name='row2Col6']").val(data.row2Col6);
		$("input[name='row2Col7']").val(data.row2Col7);
			
		$("input[name='row3Col1']").val(data.row3Col1);
		$("input[name='row3Col2']").val(data.row3Col2);
		$("input[name='row3Col3']").val(data.row3Col3);
		$("input[name='row3Col4']").val(data.row3Col4);
		$("input[name='row3Col5']").val(data.row3Col5);
		$("input[name='row3Col6']").val(data.row3Col6);
		$("input[name='row3Col7']").val(data.row3Col7);
			
		$("input[name='row4Col1']").val(data.row4Col1);
		$("input[name='row4Col2']").val(data.row4Col2);
		$("input[name='row4Col3']").val(data.row4Col3);
		$("input[name='row4Col4']").val(data.row4Col4);
		$("input[name='row4Col5']").val(data.row4Col5);
		$("input[name='row4Col6']").val(data.row4Col6);
		$("input[name='row4Col7']").val(data.row4Col7);
			
			//租金、管理费及其他
		$("input[name='row5Col1']").val(data.row5Col1);
		$("input[name='row5Col2']").val(data.row5Col2);
		$("input[name='row5Col3']").val(data.row5Col3);
		$("input[name='row5Col4']").val(data.row5Col4);
		$("input[name='row5Col5']").val(data.row5Col5);
		$("input[name='row5Col6']").val(data.row5Col6);
		$("input[name='row5Col7']").val(data.row5Col7);
		$("input[name='row5Col8']").val(data.row5Col8);
			
		$("input[name='row6Col1']").val(data.row6Col1);
		$("input[name='row6Col2']").val(data.row6Col2);
		$("input[name='row6Col3']").val(data.row6Col3);
		$("input[name='row6Col4']").val(data.row6Col4);
		$("input[name='row6Col5']").val(data.row6Col5);
		$("input[name='row6Col6']").val(data.row6Col6);
		$("input[name='row6Col7']").val(data.row6Col7);
		$("input[name='row6Col8']").val(data.row6Col8);
			
		$("input[name='total']").val(data.total);

		$("textarea[name='remark']").val(data.remark);
		
		//查看
		$("span#yearMonth").html(data.yearMonth);

		$("span#org1").html(data.org1);
			
		$("span#bank1").html(data.bank1);
			
		$("span#bankNum1").html(data.bankNum1);
			
		$("span#phone1").html(data.phone1);
			
		$("span#org2").html(data.org2);
			
		$("span#bankNum2").html(data.bankNum2);
			
		$("span#area").html(data.area);
			
			//水电费
		$("span#row1Col1").html(data.row1Col1);
		$("span#row1Col2").html(data.row1Col2);
		$("span#row1Col3").html(data.row1Col3);
		$("span#row1Col4").html(data.row1Col4);
		$("span#row1Col5").html(data.row1Col5);
		$("span#row1Col6").html(data.row1Col6);
		$("span#row1Col7").html(data.row1Col7);
			
		$("span#row2Col1").html(data.row2Col1);
		$("span#row2Col2").html(data.row2Col2);
		$("span#row2Col3").html(data.row2Col3);
		$("span#row2Col4").html(data.row2Col4);
		$("span#row2Col5").html(data.row2Col5);
		$("span#row2Col6").html(data.row2Col6);
		$("span#row2Col7").html(data.row2Col7);
			
		$("span#row3Col1").html(data.row3Col1);
		$("span#row3Col2").html(data.row3Col2);
		$("span#row3Col3").html(data.row3Col3);
		$("span#row3Col4").html(data.row3Col4);
		$("span#row3Col5").html(data.row3Col5);
		$("span#row3Col6").html(data.row3Col6);
		$("span#row3Col7").html(data.row3Col7);
			
		$("span#row4Col1").html(data.row4Col1);
		$("span#row4Col2").html(data.row4Col2);
		$("span#row4Col3").html(data.row4Col3);
		$("span#row4Col4").html(data.row4Col4);
		$("span#row4Col5").html(data.row4Col5);
		$("span#row4Col6").html(data.row4Col6);
		$("span#row4Col7").html(data.row4Col7);
			
			//租金、管理费及其他
		$("span#row5Col1").html(data.row5Col1);
		$("span#row5Col2").html(data.row5Col2);
		$("span#row5Col3").html(data.row5Col3);
		$("span#row5Col4").html(data.row5Col4);
		$("span#row5Col5").html(data.row5Col5);
		$("span#row5Col6").html(data.row5Col6);
		$("span#row5Col7").html(data.row5Col7);
		$("span#row5Col8").html(data.row5Col8);
			
		$("span#row6Col1").html(data.row6Col1);
		$("span#row6Col2").html(data.row6Col2);
		$("span#row6Col3").html(data.row6Col3);
		$("span#row6Col4").html(data.row6Col4);
		$("span#row6Col5").html(data.row6Col5);
		$("span#row6Col6").html(data.row6Col6);
		$("span#row6Col7").html(data.row6Col7);
		$("span#row6Col8").html(data.row6Col8);
			
		$("span#total").html(milliFormat(data.total));
		$("span#totalChinese").html(RapUtils.numberToChinese(data.total));
		

		$("span#remark").html(data.remark.replace("\n","<br>"));
		//特殊处理物业名称，月份，打印日期
		$("span#property").html(data.property);
		$("span#month").html((new Date(data.yearMonth).getMonth()+1)+"月份");
		var now = new Date(); 
		var nowStr = now.Format("yyyy年MM月");
		$("span#printDate").html(nowStr);
		//特殊处理数字格式
		$("#rap-form-detail .rap-td-number").each(function(i){
			var span = $("span",this);
			var text = $(span).html();
			text = milliFormat(text);
			$(span).html(text);
			$(this).css("text-align","right");
		});
	}
	function callbackContract(data){
		$("#contractId").val(data.uuid);
		$("#contractNum").val(data.contractNum);
		setPropertyAndArea(data.uuid);
		setLastMonthData();
	}
	function setLastMonthData(){
		var contractIdv = $("#contractId").val();
		var yearMonth = $("input[name='yearMonth']").val();
		var date = new Date(yearMonth);
		var month = date.getMonth();
		var lastMonth = date.getFullYear()+"-"+month;
		if(month==0){
			lastMonth = (date.getFullYear()-1)+'-12';
		}else if(month<10){
			var monthStr = "0"+month;
			lastMonth = date.getFullYear()+"-"+monthStr;
		}
		if(contractIdv!=null&&contractIdv!=''&&yearMonth!=null&&yearMonth!=''){
			var	url='${serviceurl}/query';
			$.ajax({
				url : url,
				data : {"search_EQ_contract.uuid":contractIdv,"search_EQ_yearMonth":lastMonth},
				dataType : 'json',
				success : function(data) {
					if(data.success){
						if(data.records.length>0){
							$("input[name='row1Col1']").val(data.records[0].row1Col2);
							$("input[name='row2Col1']").val(data.records[0].row2Col2);
							$("input[name='row3Col1']").val(data.records[0].row3Col2);
							$("input[name='row4Col1']").val(data.records[0].row4Col2);
						}else{
							$("input[name='row1Col1']").val("");
							$("input[name='row2Col1']").val("");
							$("input[name='row3Col1']").val("");
							$("input[name='row4Col1']").val("");
						}
					}else{
						alert(data.msg);
					}
				}
			});
		}
	}
	function setPropertyAndArea(contractId){
		var	url='${serviceurl}/getPropertyByContractId';
		$.ajax({
			url : url,
			data : {"contractId":contractId},
			dataType : 'json',
			success : function(data) {
				if(data.success){
					if(data.records.length>0){
						$("input[name='property']").val(data.records[0][0]);
						$("input[name='area']").val(data.records[0][1]);
						$("input[name='org2']").val(data.records[0][2]);
					}
				}else{
					alert(data.msg);
				}
			}
		});
	}
	function doAfterAdd(){
		$("input[name='org1']").val("深圳市深投物业发展有限公司");
		$("input[name='bank1']").val("中信银行福南支行");
		$("input[name='bankNum1']").val("7441310182600028010");
		$("input[name='phone1']").val("83883331");
		
		$("input[name='row1Col6']").val("1.16");//默认电费
		$("input[name='row2Col6']").val("3.35");//默认水费
		$("input[name='row3Col6']").val("1.20");//默认排水费
		$("input[name='row4Col6']").val("0.27");//默认垃圾处理费
		
		//说明
		//$("textArea[name='remark']").val("1.此单含lastMonth月份的水电费和thisMonth月份的租金、管理费和空调使用费；\n2.银行扣款日期：每月10日，逾期则每日按欠款金额的3‰收取违约金；");
	}
	function printTable() { 
		$("#rap-detail-toolbar").addClass("hide");
		if(document.execCommand){
			var result = document.execCommand("print", true);
			if(!result){
				window.print();
				//alert("无法启动打印，请使用快捷键“Ctrl+P”进行打印");
			}
		}else{
			alert("无法启动打印，请使用快捷键“Ctrl+P”进行打印");
		}
		$("#rap-detail-toolbar").removeClass("hide");
	}
	function bindOtherActions(){
		$("#rap-btn-print").bind("click",function(){
			printTable();
		});
		
		$("#rap-btn-ref-contractNum").colorbox({
			iframe : true,
			opacity	: 0.2,
			transition : 'none',
			speed : 200,
			href : "${ctx}/lease/contract/select?search_EQ_status=1&callback=callbackContract",
			innerWidth : '90%',
			innerHeight : '85%',
			close : '×',
			fixed : true
		});
		$('#contractNum').bind('focus', function(){
			$("#rap-btn-ref-contractNum").click();
		});
		$("input[name='yearMonth']").bind('blur', function(){
			setLastMonthData();
			
			//生成默认说明
			var yearMonth = $(this).val();
			if(yearMonth){
				var date = new Date(yearMonth);
				var lastMonth = date.getMonth();
				var thisMonth = lastMonth+1;
				if(0==lastMonth){
					lastMonth = "去年12";
				}
				$("textArea[name='remark']").val("1.此单含"+lastMonth+"月份的水电费和"+thisMonth+"月份的租金、管理费和空调使用费；\n2.银行扣款日期：每月10日，逾期则每日按欠款金额的3‰收取违约金；");
			}
		});
		//计算
		$("input[name='row1Col3']"
		+",input[name='row2Col3']"
		+",input[name='row1Col4']"
		+",input[name='row2Col4']"
		).bind('blur', function(){
			var row = $(this).closest("tr");
			calUse(row);
		});
		
		$("input[name='row2Col5']").bind('blur', function(){
			calRow3Row4();
		});
		
		$("input[name='row1Col6']"
				+",input[name='row2Col6']"
				+",input[name='row3Col6']"
				+",input[name='row4Col6']"
				).bind('blur', function(){
					var row = $(this).closest("tr");
					calMoney(row);
				});
		
		$("input[name='row1Col2']"
				+",input[name='row2Col2']"
				).bind('blur', function(){
					var row = $(this).closest("tr");
					calActual(row);
				});
		
		$("input[name='row1Col7']"
				+",input[name='row2Col7']"
				+",input[name='row3Col7']"
				+",input[name='row4Col7']"
				+",input[name='row5Col1']"
				+",input[name='row5Col2']"
				+",input[name='row5Col3']"
				+",input[name='row5Col4']"
				+",input[name='row5Col5']"
				+",input[name='row5Col6']"
				+",input[name='row5Col7']"
				+",input[name='row5Col8']"
				).bind('blur', function(){
					calTotal();
				});
	}
	function calActual(row){
		var col1 = $("input:eq(0)",row).val();
		var col2 = $("input:eq(1)",row).val();
		if(col1!=''&&col2!=''){
			var val = (parseFloat(col2)-parseFloat(col1)).toFixed(2);
			$("input:eq(2)",row).val(val);
			calUse(row);
		}
	}
	function calUse(row){
		var col3 = $("input:eq(2)",row).val();
		var col4 = $("input:eq(3)",row).val();
		col3=col3==''?0:parseFloat(col3);
		col4=col4==''?0:parseFloat(col4);
		var val = (parseFloat(col3)+parseFloat(col4)).toFixed(2);
		$("input:eq(4)",row).val(val);
		calMoney(row);
		if($("input:eq(4)",row).attr("name")=="row2Col5"){
			calRow3Row4();
		}
	}
	function calRow3Row4(){
		var val = $("input[name='row2Col5']").val();
		if(val!=''){
			$("input[name='row3Col5']").val((val*0.9).toFixed(2));
			$("input[name='row4Col5']").val((val*0.9).toFixed(2));
			
			calMoney($("input[name='row3Col5']").closest("tr"));
			calMoney($("input[name='row4Col5']").closest("tr"));
		}
	}
	function calMoney(row){
		var col5 = $("input:eq(4)",row).val();
		var col6 = $("input:eq(5)",row).val();
		if(col5!=''&&col6!=''){
			var val = (parseFloat(col5)*parseFloat(col6)).toFixed(2);
			 $("input:eq(6)",row).val(val);
			 calTotal();//计算总金额
		}
	}
	function calTotal(){
		var val1 = $("input[name='row1Col7']").val();
		var val2 = $("input[name='row2Col7']").val();
		var val3 = $("input[name='row3Col7']").val();
		var val4 = $("input[name='row4Col7']").val();
		var val5 = $("input[name='row5Col1']").val();
		var val6 = $("input[name='row5Col2']").val();
		var val7 = $("input[name='row5Col3']").val();
		var val8 = $("input[name='row5Col4']").val();
		var val9 = $("input[name='row5Col5']").val();
		var val10 = $("input[name='row5Col6']").val();
		var val11 = $("input[name='row5Col7']").val();
		var val12 = $("input[name='row5Col8']").val();
		
		val1=val1==''?0:parseFloat(val1);
		val2=val2==''?0:parseFloat(val2);
		val3=val3==''?0:parseFloat(val3);
		val4=val4==''?0:parseFloat(val4);
		val5=val5==''?0:parseFloat(val5);
		val6=val6==''?0:parseFloat(val6);
		val7=val7==''?0:parseFloat(val7);
		val8=val8==''?0:parseFloat(val8);
		val9=val9==''?0:parseFloat(val9);
		val10=val10==''?0:parseFloat(val10);
		val11=val11==''?0:parseFloat(val11);
		val12=val12==''?0:parseFloat(val12);
		
		
		var totalMoney = (val1+val2+val3+val4+val5+val6+val7+val8+val9+val10+val11+val12).toFixed(2);
		
		$("input[name='total']").val(totalMoney);
		
	}
	$(document).ready(function() {
		bindDefaultActions();
		bindOtherActions();
		_queryData = $("#rap-form-query").serializeArray();
		if(_isServerPage){
			//服务器分页
			serverPage();	
		}else{
			//非服务器分页
			_tableList = $('#rap-table-list').DataTable({
				"columns" : _tableCols,
				"order": [] 
			});
			loadList();		
		}
		
		RapDataTableUtils.setCheckCol(_tableList);

		/* 常用校验
		isRightfulString:true //非特殊字符 一般用于编码字段
		maxLength:200 //长度
		number:true //阿拉伯数字
		isNumber:true //匹配数值类型，包括整数和浮点数
		isFloat:true //浮点数
		isPhone:true //手机号码
		//其他校验的参考 rap-form-utils中的设置
		*/
		_validator = $('#rap-form-edit').validate({
					rules : {
						uuid : {
							required : true //必填
						},
						yearMonth : {
							required : true //必填
						},
						property : {
							required : true //必填
						},
						contractNum : {
							required : true //必填
						}
					},
					messages : {
					}
				});
	});
</script>